/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.appspot.trick.it.admin;

import com.appspot.trick.it.admin.controller.AddSiteController;
import com.appspot.trick.it.admin.controller.MainController;
import com.appspot.trick.it.admin.controller.OAuthController;
import java.io.IOException;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.application.HostServices;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 *
 * @author takahara
 */
public class TrickAdmin extends Application {
    
    /**
     * Comas instance
     */
    private static TrickAdmin instance;
    /**
     * ステージ
     */
    private Stage stage;

    /**
     * 承認ステージ
     */
    private Stage oauthStage;
    
    /**
     * サイト追加ステージ
     */
    private Stage addSiteStage;
    
    /**
     * 記事ステージ
     */
    private Stage articleStage;
    
    /**
     * パラメーターステージ
     */
    private Stage parameterStage;
    
    /**
     * パラメーターステージ
     */
    private Stage addHTMLSauceStage;
    
    /**
     * Host Services
     */
    private HostServices hostServices;
    
    /**
     * コンストラクタ
     */
    public TrickAdmin() {
        this.setInstance();
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        stage.setWidth(400);
        stage.setHeight(900);
//        stage.getIcons().add(new Image(Comas.class.getResourceAsStream("images/drive-trick-logo.png")));
        
        sendMainController();
        stage.show();
        
        hostServices = getHostServices();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    /**
     * インスタンスの設定
     */
    private void setInstance() {
        instance = this;
    }
    
    /**
     * メイン画面へ遷移する
     */
    public void sendMainController() {

        stage.setTitle(Constants.APPLICATION_NAME);
        stage.getIcons().add(new Image(TrickAdmin.class.getResourceAsStream("images/logo.png")));

        MainController controller = new MainController();
        this.replaceSceneContent(controller);
    }
    
    /**
     * OAuth承認画面を表示
     * @param controller
     * @throws java.io.IOException
     */
    public void showOAuthStage(OAuthController controller) throws IOException {
        
        // 承認用ステージの初期化
        if (oauthStage == null) {
            oauthStage = new Stage();

            oauthStage.initOwner(this.getStage());
            oauthStage.initModality(Modality.APPLICATION_MODAL);
            oauthStage.setTitle("アプリ承認");
            oauthStage.getIcons().add(new Image(TrickAdmin.class.getResourceAsStream("images/logo.png")));
            oauthStage.setWidth(650);
            oauthStage.setHeight(600);
        }
        
        
        Scene scene = oauthStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            oauthStage.setScene(scene);
        } else {
            oauthStage.getScene().setRoot(controller);
        }

        oauthStage.show();
    }
    
    /**
     * サイト追加画面を表示
     * @param controller
     * @throws java.io.IOException
     */
    public void showAddSiteStage(AddSiteController controller) throws IOException {
        
        // 承認用ステージの初期化
        if (addSiteStage == null) {
            addSiteStage = new Stage();

            addSiteStage.initOwner(this.getStage());
            addSiteStage.initModality(Modality.APPLICATION_MODAL);
            addSiteStage.setTitle("アプリ承認");
            addSiteStage.getIcons().add(new Image(TrickAdmin.class.getResourceAsStream("images/logo.png")));
            addSiteStage.setWidth(500);
            addSiteStage.setHeight(300);
        }
        
        
        Scene scene = addSiteStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            addSiteStage.setScene(scene);
        } else {
            addSiteStage.getScene().setRoot(controller);
        }

        addSiteStage.show();
    }
    
    /**
     * プレービューステージを表示
     * @param controller
     * @param title
     */
    public void showArticleStage(Pane controller, String title) {
        
        if (articleStage == null) {
            articleStage = new Stage();

            articleStage.initOwner(this.getStage());
            articleStage.initModality(Modality.APPLICATION_MODAL);
            articleStage.getIcons().add(new Image(TrickAdmin.class.getResourceAsStream("images/logo.png")));
            articleStage.setWidth(806);
            articleStage.setHeight(600);
        }
        
        articleStage.setTitle(title);
        
        
        Scene scene = articleStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            articleStage.setScene(scene);
        } else {
            articleStage.getScene().setRoot(controller);
        }

        articleStage.show();
    }
    
    /**
     * プレービューステージを表示
     * @param controller
     */
    public void showParameterStage(Pane controller) {
        
        if (parameterStage == null) {
            parameterStage = new Stage();

            parameterStage.initOwner(this.getStage());
            parameterStage.initModality(Modality.APPLICATION_MODAL);
            parameterStage.getIcons().add(new Image(TrickAdmin.class.getResourceAsStream("images/logo.png")));
            parameterStage.setWidth(806);
            parameterStage.setHeight(600);
        }
        
        parameterStage.setTitle("サイトの設定");
        
        
        Scene scene = parameterStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            parameterStage.setScene(scene);
        } else {
            parameterStage.getScene().setRoot(controller);
        }

        parameterStage.show();
    }
    
    /**
     *HTML追加テージを表示
     */
    public void showAddHTMLSauceStage(Pane controller) {
        
        if (addHTMLSauceStage == null) {
            addHTMLSauceStage = new Stage();

            addHTMLSauceStage.initOwner(articleStage);
            addHTMLSauceStage.initModality(Modality.APPLICATION_MODAL);
            addHTMLSauceStage.getIcons().add(new Image(TrickAdmin.class.getResourceAsStream("images/logo.png")));
            addHTMLSauceStage.setWidth(500);
            addHTMLSauceStage.setHeight(500);
        }
        
        addHTMLSauceStage.setTitle("HTML追加");
        
        Scene scene = addHTMLSauceStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            addHTMLSauceStage.setScene(scene);
        } else {
            addHTMLSauceStage.getScene().setRoot(controller);
        }

        addHTMLSauceStage.show();
    }
    
    /**
     * HTML追加画面を閉じる
     */
    public void closeAddHTMLSauceStageStage() {
        addHTMLSauceStage.close();
    }
    
    /**
     * OAuth承認画面を閉じる
     */
    public void closeOAuthStage() {
        oauthStage.close();
    }
    
    /**
     * サイト追加画面を閉じる
     */
    public void closeAddSiteStage() {
        addSiteStage.close();
    }
    
    /**
     * メインステージの取得
     *
     * @return
     */
    public Stage getStage() {
        return stage;
    }
    
    /**
     * Get App Instance
     *
     * @return
     */
    public static TrickAdmin getInstance() {
        return instance;
    }
    
    /**
     * ページ遷移
     *
     * @param controller
     */
    private void replaceSceneContent(Parent controller) {
        Scene scene = stage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            stage.setScene(scene);
        } else {
            stage.getScene().setRoot(controller);
        }
    }
    
    /**
     * JavaFX Host Services の取得
     * @return 
     */
    public HostServices getJavaFXHostServices() {
        return this.hostServices;
    }
    
    /**
     * 記事ステージの取得
     * @return 
     */
    public Stage getArticleStage() {
        return this.articleStage;
    }
    
}
